<template>
	<div>
		<div v-if="showoerr" class="showoerr">{{wrongmsg}}</div>
		<Home v-if="showhome" />
		<div class="loadingDiv" v-loading="$store.state.route.loading" element-loading-text="加载中…"
			element-loading-spinner="el-icon-loading" element-loading-background="rgba(255, 255, 255, 0.8)"></div>
	</div>
</template>

<script>
	import {
		commonPost
	} from "@api/data";

	import {
		getHomeConf, //查询主页面医院配置
		getSstNotice
	} from "@api/apiBox/terminal"

	import {
		getHomePage
	} from "@api/apiBox/scene"

	import Home from "./Home";
	import {
		CallMedIns
	} from "@api/tools/MedInsTrade.js";
	import moment from "moment";
	import {
		SystemSet
	} from '@api/tools/Hardware.js'
	import {
		MessageBox
	} from "element-ui";
	// import ZzfkUrl from "@images/zhuce.png";
	// import MzghUrl from "@images/clock.png";
	// import MzcxUrl from "@images/list.png";
	// import MzjsUrl from "@images/pay.png";
	// import MzczUrl from "@images/withdrawal.png";
	// import OthUrl from "@images/more.png";
	// import PriUrl from "@images/printer.png";
	// import ZyczUrl from "@images/ihpay.png";
	// import QdcxUrl from "@images/qdcx.png";
	export default {
		name: "homepage",
		data() {
			return {
				YB: new CallMedIns(),
				System: new SystemSet(),
				showoerr: false,
				showhome: false,
				isVerticalScreen: false,
				wrongmsg: "",
				terminfo: [],
				seceninfo: [],
				funcinfo: [],
				timer: '',
				ip: '',
				//关机轮询
				shutdownTimeInter: '',
				//最后点击时间
				lastClickTime: ''
			};
		},
		methods: {
			//医保初始化
			async ybsign() {
				var CmiSign = '';
				var CmiBusinessInfo = '';
				var datetime = moment().format("YYYYMMDDHHmmSS");
				let orginfo = this.$store.state.terminfo.orginfo;
				let termId = this.$store.state.terminfo.terminfo.termId;
				var operno = orginfo.cmiid + termId.substr(2, 1) + termId.substr(4, 2);
				this.$store.dispatch("terminfo/updateyboperno", operno);
				// eslint-disable-next-line no-console
				console.log(
					"res=" + datetime + "；cmiid=" + orginfo.cmiid + ";operno=" + this.$store.state.terminfo
					.yboperno
				);
				if (orginfo.cmiid === '104') {
					CmiSign = "9100^" + orginfo.cmiid + "^" + operno + "^^" + datetime + orginfo.cmiid +
						"001^0000^^1^";
					// eslint-disable-next-line no-console
					console.log("开始签到" + CmiSign);
					let temp = await this.YB.Sign(1, CmiSign);
					let temp2 = temp.split("^")[2];
					CmiBusinessInfo = temp2.split("|")[0];
					// eslint-disable-next-line no-console
					console.log("CmiBusinessInfo=" + CmiBusinessInfo);
				} else {
					CmiSign = "9100^" + orginfo.cmiid + "^" + operno + "^^" + datetime + orginfo.cmiid + "001^^^";
					// eslint-disable-next-line no-console
					console.log("开始签到" + CmiSign);
					let temp = await this.YB.Sign(1, CmiSign);
					CmiBusinessInfo = temp.split("^")[1];
					// eslint-disable-next-line no-console
					console.log("CmiBusinessInfo=" + CmiBusinessInfo);
				}
				this.$store.dispatch("terminfo/updatemibusinessinfo", CmiBusinessInfo);

				// var PmiSign='9100^'+orginfo.cmiid+'^'+termId+'^^'+datetime+orginfo.cmiid+'002^229900^^1^';
				// let PMIBusinessInfo= this.YB.MISign(2,'省医保签到报文');
			},
			async ybinit() {
				this.YB.Init(1).then(() => this.ybsign()).catch(e => {
					console.error("医保初始化失败");
				});
			},
			systemInit() { //系统初始化请求系列配置
				let _this = this;
				let ip = this.ip;
				let data = {
					data: {
						ip: ip
					},
					that: _this
				};
				_this.$store.dispatch("terminfo/updateipAddress", ip); //存储IP地址
				//查询系统配置信息
				getHomeConf(data)
					.then(res => {
						console.log(res, 'res')
						//存储系统配置信息
						_this.dispatchData(res);
						//获取系统配置信息
						_this.getHomePageMethod(res);
					})
					.catch(err => {
						console.log(err, 'err')
						_this.showoerr = true;
						_this.wrongmsg = "服务异常，请联系管理员处理！";
					});
			},
			//获取系统配置信息
			getHomePageMethod(res) {
				let _this = this;
				let data = {
					data: {
						sceneId: res.data.sstSceneVO.id
					},
					that: _this
				};
				//获取场景功能信息接口
				getHomePage(data)
					.then(SceneRes => {
						_this.$store.dispatch("terminfo/updatefuncinfo", SceneRes.data); //场景功能信息  mzyy mzgh ..
						console.log(SceneRes, '获取场景功能信息接口')
						this.showhome = true;
						this.showoerr = false;
					})
					.catch(err => {
						console.log(err, 'err')
						_this.showoerr = true;
						_this.wrongmsg = "服务异常，请联系管理员处理！";
					});
			},
			//存储信息
			async dispatchData(res) {
				console.log(res, '查询系统配置信息')
				// 	console.log("sysDateTime=" + res.data.sysDateTime);
				this.$store.dispatch("terminfo/updatehospName", res.data.hospName); //医院名称
				this.$store.dispatch("terminfo/updatehospLogo", res.data.hospUrl); //医院logo
				this.$store.dispatch("terminfo/updatehospId", res.data.hospId); //医院Id
				// 	this.$store.dispatch("terminfo/updateorginfo", res.data.orginfo); //机构信息
				this.$store.dispatch("terminfo/updateseceninfo", res.data.sstSceneVO); //场景信息  场景对象
				// this.setShowDown();
				// 	console.log("yibao=" + res.data.orginfo.cmiid);
				// 	if (res.data.orginfo.cmiid !== "" || res.data.orginfo.cmiid != undefined) { //医保编码不为空则进行医保初始化
				// 		this.ybinit();
				// 	}
				this.System.SysSetTime(new Date()); //设置本机时间与服务器时间一致。
				let sstTerminal = res.data.sstTerminalVO;
				this.$store.dispatch("terminfo/updatedeviceConfs", res.data.sstTerminalDeviceVO); //终端硬件信息
				sessionStorage.setItem("terminfo", JSON.stringify(res.data.sstTerminalDeviceVO)); //终端硬件信息
				this.$store.dispatch("terminfo/updateterminfo", sstTerminal); //终端信息  终端对象

				//获取公告信息
				let sstNoticeResData = {
					data: {
						termId: sstTerminal.termId
					},
					that: this
				}
				const getSstNoticeRes = await getSstNotice(sstNoticeResData);
				let word = "";
				if (getSstNoticeRes.data.length > 0) {
					for (let item of getSstNoticeRes.data) {
						word += item.noticeContent;
					}
				}
				console.log('word', word)
				this.$store.dispatch("terminfo/updateReportWordk", word); //终端信息  终端对象
				console.log('存储后的', this.$store.state)
        console.log('打印的对象reData', localStorage.getItem("reData"))
        console.log('打印的对象appointmentInfo', localStorage.getItem("appointmentInfo"))
        console.log('打印的对象data', localStorage.getItem("data"))
        console.log('打印的对象hisVisitNo', localStorage.getItem("hisVisitNo"))
        console.log('打印的对象successRest', localStorage.getItem("successRest"))
			},
			//设置关机时间d
			async setShowDown() {
				let date = this.$store.state.terminfo.seceninfo.shutdownTime

				let d = new Date(date);

				let hours = d.getHours();

				let min = d.getMinutes();

				let sec = d.getSeconds();

				let endTime = hours + ':' + min + ':' + sec;
				console.log('自助机关机时间：' + endTime)
				clearInterval(this.shutdownTimeInter)
				this.shutdownTimeInter = window.setInterval(() => {
					const now = new Date()
					//到达关机时间并且60秒内没有人点击则关机
					if ((now.getHours() > hours || (now.getHours() == hours && now.getMinutes() >= min)) && ((
							now.getTime() - this.lastClickTime > 60000) || this.lastClickTime == "")) {
						console.log('关机')
						this.shutDown();
						clearInterval(this.shutdownTimeInter)
						return;
					}
				}, 60000);
				//监听点击时间 如果点击了则更新最后点击时间
				window.addEventListener('click', () => {
					this.lastClickTime = new Date().getTime();
				})
			},
			//关机
			async shutDown() {
				try {
					await this.System.SysShutDown();
				} catch (e) {
					await this.System.SysShutDown();
				}
			}
		},
		mounted() {

		},
		async created() {
			//获取本地ipv4地址
			var ipJson = "";
			try {
				ipJson = await this.System.doInterface();
				if (ipJson.length > 0) {
					this.ip = ipJson[0];
				}
			} catch (e) {
				if (this.$global.isDevlopment) {
					// 如果报错则为本地测试地址
					this.ip = '127.0.0.1';
				}
			}
			console.log(ipJson)
			var sceneid = this.$store.state.terminfo.seceninfo.id;
			if (sceneid !== undefined) { //不为空则进行刷新更新，为空则是第一次启动加载
				location.reload();
			} else {
				this.systemInit();
			}
		},
		beforeCreate() {

		},
		computed: {
			// 计算属性
			statusData() {
				return this.showoerr;
			}
		},
		watch: {
			statusData: function(showoerr) {
				// 当返回的新值为创建中的时候,保持3秒轮询
				if (showoerr) {
					this.timer = setInterval(() => {
						setTimeout(this.systemInit, 100);
					}, 30000);
				}
				// 当请求正常时
				if (!showoerr) {
					//关闭弹窗
					MessageBox.close();
					clearInterval(this.timer);
					this.timer = "";
				}
			}
		},
		components: {
			Home
		}
	};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
	@import "../assets/css/homePage.scss";
</style>
